package com.qf.conf;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

public class LoadBalancerConfig {

    @Bean
    public ReactorServiceInstanceLoadBalancer loadBalancer(Environment env, LoadBalancerClientFactory clientFactory){

        String serviceName = env.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);

        ObjectProvider<ServiceInstanceListSupplier> provider = clientFactory.getLazyProvider(serviceName, ServiceInstanceListSupplier.class);

        return new RoundRobinLoadBalancer(provider,serviceName);
    }
}
